import 'package:flutter/material.dart';
import 'http.dart';

class WeatherPage extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => new _WeatherPageState();
}

class _WeatherPageState extends State<WeatherPage>{
  var _lakeImageUrl = "";
  var _weatherResult = new Map();
  final TextEditingController _controller = new TextEditingController(text: '101060101');

  _getWeather(String citycode) async{
    var result = await getData(
      url:'/apis/weather/',
      params: {
        'id': citycode
      }
    );
    if(!mounted) return;
    if(result['success'] == false){
      showDialog(
        context: context,
        builder: (context){
          return AlertDialog(
            title: new Text('错误'),
            content: new Text(result['reason']),
          );
        }
      );
      return;
    }
    setState(() {
      // _lakeImageUrl = "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1407/14/c1/36282965_1405312627685.jpg";
      _weatherResult = result['result'];
      _lakeImageUrl = "http:${_weatherResult['weatherimg']}";
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getWeather(_controller.text);
  }

  @override
  Widget build(BuildContext context) {
    Widget inputSection = new Row(
      children: <Widget>[
        new Expanded(
          child: new TextField(
            controller: _controller,
            decoration: new InputDecoration(
              hintText: '输入城市代码',
            ),
          )
        ),
        new RaisedButton(
          child: new Text("查询"),
          onPressed: (){
            // showDialog(
            //   context: context,
            //   child: new AlertDialog(
            //     title: new Text('城市代码'),
            //     content: new Text(_controller.text)
            //   )
            // );
            _getWeather(_controller.text);
          }
        )
      ]
    );
    Widget textSection = new Container(
      padding: const EdgeInsets.all(32.0),
      child: new Text(
        '''
        城市：${_weatherResult['city']}
        citycode：${_weatherResult['citycode']}
        天气：${_weatherResult['weather']}
        风向：${_weatherResult['wd']}
        风力：${_weatherResult['wdforce']}
        风速：${_weatherResult['wdspd']}
        气压：${_weatherResult['stp']}
        能见度：${_weatherResult['wisib']}
        湿度：${_weatherResult['humidity']}
        降雨：${_weatherResult['prcp']}
        24小时降雨量：${_weatherResult['prcp24h']}
        AQI：${_weatherResult['aqi']}
        pm25：${_weatherResult['pm25']}
        当前时间：${_weatherResult['today']}
        ''',
        softWrap: true,
      ),
    );
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("天气预报页"),
      ),
      body: new ListView(
        children:[
          inputSection,
          new FadeInImage.assetNetwork(
            placeholder: 'images/lake.jpeg', 
            image: _lakeImageUrl,
            width: 600.0,
            height:240.0,
            fit: BoxFit.cover,
          ),
          textSection,
        ]
      ),
    );
  }

}